package com.ijunfu.safe._02_lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @Title  : 
 * @Author : ijunfu <ijunfu@163.com>
 * @Date   : 2023/11/16 21:32
 * @Version: 1.0
 * @Motto  : 世界很大 世界很小
 *
 */
@Slf4j
public class LockTest {

    static ReentrantLock lock = new ReentrantLock(Boolean.TRUE);
    public static int count = 10;

    public static Boolean handle() {
        try {
            lock.lock();
            if(count<=0) {
                return Boolean.FALSE;
            }
            count--;
            log.info("{}", count);
        } finally {
            lock.unlock();
        }
        return Boolean.TRUE;
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while(handle()){}
        });

        Thread t2 = new Thread(()->{
            while(handle()){}
        });

        t1.start();
        t2.start();
    }
}
